interface ApiConfig {
  name: string
  baseUrl: string
  proxyUrl: string
}

// 从环境变量自动生成API配置
const apiConfigs: ApiConfig[] = [
  {
    name: 'main',
    baseUrl: import.meta.env.VITE_MAIN_API_BASE_URL,
    proxyUrl: import.meta.env.VITE_MAIN_API_PROXY_URL
  },
  {
    name: 'auth',
    baseUrl: import.meta.env.VITE_AUTH_API_BASE_URL,
    proxyUrl: import.meta.env.VITE_AUTH_API_PROXY_URL
  },
  {
    name: 'upload',
    baseUrl: import.meta.env.VITE_UPLOAD_BASE_URL,
    proxyUrl: import.meta.env.VITE_UPLOAD_PROXY_URL
  }
]

// 生成代理配置
export const proxyConfig = apiConfigs.reduce((config, api) => {
  config[api.baseUrl] = {
    target: api.proxyUrl,
    changeOrigin: true,
    rewrite: (path: string) => path.replace(new RegExp(`^${api.baseUrl}`), '')
  }
  return config
}, {} as Record<string, any>)

// 导出API基础配置
export const hosts = {
  baseUrl: import.meta.env.VITE_API_BASE_URL,
  timeout: parseInt(import.meta.env.VITE_API_TIMEOUT || '30000'),
  apis: apiConfigs.reduce((config, api) => {
    config[api.name] = api.baseUrl
    return config
  }, {} as Record<string, string>)
}
